package net.airtheva.uniqueTask2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	EditText mUsername = null;
	EditText mPassword = null;
	Button mLogin = null;
	Button mRegister = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		mUsername = (EditText) findViewById(R.id.activity_login_username);
		
		mPassword = (EditText) findViewById(R.id.activity_login_password);
		
		mLogin = (Button) findViewById(R.id.activity_login_login);
		mLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String username = mUsername.getText().toString();
				String password = mPassword.getText().toString();
				
				API api = API.GetInstance(LoginActivity.this);
				Response res = api.Login(username, password);
				
				switch(res.GetStatusCode()) {
				case Response.STATUS_CODE_SUCCESS:
					SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.putString("username", username);
					editor.putString("password", password);
					editor.commit();
					
					Intent intent = new Intent(LoginActivity.this, UserActivity.class);
					intent.putExtra("session", res.GetSession());
					intent.putExtra("user", res.GetExtra());
					startActivity(intent);
					break;
				default:
					Toast.makeText(LoginActivity.this, res.GetExtra(), Toast.LENGTH_SHORT).show();
					break;
				}
				
			}
		});
		
		mRegister = (Button) findViewById(R.id.activity_login_register);
		mRegister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = mUsername.getText().toString();
				String password = mPassword.getText().toString();
				
				API api = API.GetInstance(LoginActivity.this);
				Response res = api.Register(username, password);
				
				switch(res.GetStatusCode()) {
				case Response.STATUS_CODE_SUCCESS:
					Toast.makeText(LoginActivity.this, "注册成功，来完善一下个人信息啦！", Toast.LENGTH_SHORT).show();
					SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = sharedPreferences.edit();
					editor.putString("username", username);
					editor.putString("password", password);
					editor.commit();
					
					Intent intent = new Intent(LoginActivity.this, SetupActivity.class);
					intent.putExtra("session", res.GetSession());
					intent.putExtra("user", res.GetExtra());
					startActivity(intent);
					break;
				default:
					Toast.makeText(LoginActivity.this, res.GetExtra(), Toast.LENGTH_SHORT).show();
					break;
				}
				
			}
			
		});
		
		SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
		mUsername.setText(sharedPreferences.getString("username", ""));
		mPassword.setText(sharedPreferences.getString("password", ""));
		
	}

}
